package com.sky.controller.user;

import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/27 0:23
 */
@RestController(value = "userSetmealController")
@Api(tags = "用户端套餐相关接口")
@RequestMapping(value = "/user/setmeal")
@Slf4j
public class SetmealController {
    @Resource
    SetmealService setmealService;

    /**
     * 根据分类id查询套餐
     *  @Cacheable注解作用：
     *  1. 执行该方法前，先去redis缓存中查找key = setmealCache::categoryId，如果存在，则直接返回，如果不存在，则继续执行该方法
     *  2. 如果缓存不存在，则在该方法执行完毕后将方法的返回值存入redis缓存中
     * @param categoryId
     * @return
     */
    @Cacheable(cacheNames = "setmealCache",key = "#categoryId")
    @ApiOperation(value = "根据分类id查询套餐")
    @GetMapping(value = "/list")
    public Result<List<Setmeal>> getByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealService.getByCategoryId(categoryId);
        return Result.success(setmeals);
    }

    /**
     * 根据套餐id查询包含的菜品
     * @return
     */
    @GetMapping(value = "/dish/{id}")
    public Result<List<DishItemVO>> getDishBySetmealId(@PathVariable(value = "id") Long id){
        List<DishItemVO> dishItemVOS = setmealService.getDishBySetmealId(id);
        return Result.success(dishItemVOS);
    }
}
